﻿using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint;


namespace Eneta.Portal.Profiles
{
    public class LogInControl : UserControl
    {
        public static string ShowLogIn(HttpContext context)
        {
            var user = SPContext.Current.Web.CurrentUser;
            string controlName;

            if (user != null)
                controlName = "AuthenticatedLogInControl.ascx";
            else
                controlName = "AnonymousLogInControl.ascx";
            
            var path = "~/_controltemplates/" + controlName;
            var output = new StringBuilder(10000);

            using(var page = new Page())
            using(var ctl = page.LoadControl(path))
            using(var writer = new StringWriter(output))
            using (var htmlWriter = new HtmlTextWriter(writer))
            {
                ctl.DataBind();
                ctl.RenderControl(htmlWriter);
            }
            return output.ToString();
        }

        protected override void OnPreRender(System.EventArgs e)
        {            
            base.OnPreRender(e);


            //Response.Write("<!--\r\n");
            //for (Control control = Parent; control != null; control = control.Parent)
            //{
            //    var type = control.GetType();
            //    if (type.Namespace == "ASP")
            //        type = type.BaseType;

            //    Response.Write(control.ID + ": " + type);
            //    if (control is BasePartialCachingControl)
            //    {
            //        Response.Write(" Partial");
            //    }
            //    Response.Write("\r\n");
            //}
            //Response.Write("-->");          
        }
    }
}
